package com.kuang.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockDemo {public static void main(String[] args) {
    Phone phone = new Phone();
    new Thread(()->{
        phone.sms();
    },"A").start();
    new Thread(()->{
        phone.sms();
    },"B").start();
}
}
class Phone{
    private Lock lock=new ReentrantLock();
    public  void sms(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "sms");
            call(); // 这里也有锁
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }
    public  void call(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "call");

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
